/*
 *******************************************************************
 *
 * COPYRIGHT (coffee) 2013 - 2014 LTV Co., Ltd  All rights reserved
 *
 * Created  : Mar 23, 2014
 * Change History
 * =================================================================
 * DATE                DEVELOPER        REVISION      DESCRIPTION
 * =================================================================
 * Mar 23, 2014        ducnguyen        1.0           First Created.
 * =================================================================
 *
 *
 *******************************************************************/

package vn.com.ltv.lo.daos;

import java.util.ArrayList;

import vn.com.ltv.lo.dtos.FoodDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class FoodDAO {
	protected static final String TABLE_NAME = "foods";
	protected static final String ID = "_id";
	protected static final String NAME = "name";
	protected static final String IMAGE = "image";
	protected static final String PRICE = "price";
	protected static final String RATE = "rate";
	protected static final String ID_CATEGORY = "id_category";
	protected static String TABLE_CREATION_STRING = "create table "
			+ TABLE_NAME + " (" + ID + " integer primary key autoincrement, "
			+ NAME + " text, " + IMAGE + " text, " + PRICE + " integer, "
			+ RATE + " integer, " + ID_CATEGORY + " integer);";
	protected static String TABLE_DROP_STRING = "drop table if exists"
			+ TABLE_NAME + ";";
	private Context context;

	public FoodDAO(Context context) {
		this.context = context;
	}

	public ArrayList<FoodDTO> getAll() {
		ArrayList<FoodDTO> foodDTOs = new ArrayList<FoodDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, null, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				FoodDTO foodDTO = this.cursorToFoodDTO(cursor);
				foodDTOs.add(foodDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return foodDTOs;
	}
	
	public ArrayList<FoodDTO> getAllByCategoryId(int id) {
		ArrayList<FoodDTO> foodDTOs = new ArrayList<FoodDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, ID_CATEGORY + "=?", new String[] { String.valueOf(id) },
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				FoodDTO foodDTO = this.cursorToFoodDTO(cursor);
				foodDTOs.add(foodDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return foodDTOs;
	}

	public FoodDTO get(int id) {
		FoodDTO foodDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			foodDTO = this.cursorToFoodDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return foodDTO;
	}

	public void insert(FoodDTO foodDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		ContentValues contentValues = this.foodDTOToContentValues(foodDTO);
		sQLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	/** * @return the number of rows affected */
	public int update(FoodDTO foodDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		int id = foodDTO.getId();
		ContentValues contentValues = this.foodDTOToContentValues(foodDTO);
		int rowCount = sQLiteDatabase.update(TABLE_NAME, contentValues, ID
				+ "=?", new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
		return rowCount;
	}

	public void delete(int id) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		sQLiteDatabase.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	private FoodDTO cursorToFoodDTO(Cursor cursor) {
		FoodDTO foodDTO = new FoodDTO();
		int id = cursor.getInt(cursor.getColumnIndex(ID));
		foodDTO.setId(id);
		String name = cursor.getString(cursor.getColumnIndex(NAME));
		foodDTO.setName(name);
		String image = cursor.getString(cursor.getColumnIndex(IMAGE));
		foodDTO.setImage(image);
		int price = cursor.getInt(cursor.getColumnIndex(PRICE));
		foodDTO.setPrice(price);
		int rate = cursor.getInt(cursor.getColumnIndex(RATE));
		foodDTO.setRate(rate);
		int idCategory = cursor.getInt(cursor.getColumnIndex(ID_CATEGORY));
		foodDTO.setIdCategory(idCategory);
		return foodDTO;
	}

	private ContentValues foodDTOToContentValues(FoodDTO foodDTO) {
		ContentValues contentValues = new ContentValues();
		int id = foodDTO.getId();
		if (id >= 0) {
			contentValues.put(ID, id);
		}
		String name = foodDTO.getName();
		if (name != null) {
			contentValues.put(NAME, name);
		}
		String image = foodDTO.getImage();
		if (image != null) {
			contentValues.put(IMAGE, image);
		}
		int price = foodDTO.getPrice();
		if (price >= 0) {
			contentValues.put(PRICE, price);
		}
		float rate = foodDTO.getRate();
		if (rate >= 0) {
			contentValues.put(RATE, rate);
		}
		int idCategory = foodDTO.getIdCategory();
		if (idCategory >= 0) {
			contentValues.put(ID_CATEGORY, idCategory);
		}
		return contentValues;
	}
}
